﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RockCompany.Common.Lib;
using RockCompany.Common.Constant;
using RockCompany.Master.Model;
using RockCompany.Master.Controller;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using RockCompany.Master.Validator;
using RockCompany.Master.View;
using RockCompany.Common.Forms;

namespace RockCompany.Master.View
{
    public partial class SI06A : ParentFrm
    {
        #region Properties
        private UserAuthenticate uAuth;
        private Translator trans;
        private SI06FormModel frmModel;
        private DataGridViewRow selectRow = null;
        private bool isPassed = false;
        private bool isNew = true;
        private SI05B frm_PhieuXH;
        private string sUserName;
        #endregion

        #region Constructor
        public SI06A(Translator translator, string sUName, Form caller)
            : base(sUName, caller)
        {
            InitializeComponent();            
            sUserName = sUName;            
            this.trans = translator;
        }

        #endregion

        #region Event Methods
        private void button_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_NewProdExportDetail_Click(object sender, EventArgs e)
        {
            this.Hide();
            frm_PhieuXH = new SI05B(trans, sUserName, this);
            frm_PhieuXH.callChooser(true);
            if (frm_PhieuXH.ShowDialog(this) == DialogResult.OK)
            {
                DataTable selectedPhieuXH = frm_PhieuXH.getSelectedProdEx();
                frm_PhieuXH.Dispose();
                List<string> lst = new List<string>();
                lst.Add(PHIEU_XK_TP_CT_DTO.N_SO_LO);
                lst.Add(PHIEU_XK_TP_CT_DTO.N_MA_MAU);
                lst.Add(PHIEU_XK_TP_DTO.N_MS_PHIEU_XH);
                frmModel.copyDataTable(selectedPhieuXH, frmModel.newProdExportDetail, lst);
                CommonModel.bindDGV(dgvProdductList, frmModel.newProdExportDetail);
            }
        }

        private void button_DelProdExportDetail_Click(object sender, EventArgs e)
        {
            if (frmModel.removeProdExportDetail(textBox_MSCT.Text))
            {
                InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
            }
            else
                InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
        }

        private void button_SaveProdExport_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox_MSCT.Text) &&
                InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION)
                                                                         , trans.getMessageCaption(CommonConstantIF.SI06_ADD_CONFIRM)))
            {
                PHIEU_XK_TP_DTO curProdExport = new PHIEU_XK_TP_DTO();
                curProdExport.MS_PHIEU_XK_TP = textBox_MSCT.Text;
                curProdExport.NGAY_XK = picNgayXK.Value.Date;
                curProdExport.PHE_DUYET = isPassed;
                curProdExport.CREATION_USER = (string.IsNullOrEmpty(sUserName) ? "" : sUserName);
                curProdExport.LAST_UPDATE_USER = (string.IsNullOrEmpty(sUserName) ? "" : sUserName);

                if (!isNew)
                {
                    bool b1 = frmModel.updateProdExport(curProdExport); ;
                    bool b2 = frmModel.saveData(textBox_MSCT.Text, sUserName);
                    if (b1 && b2)
                    {
                        InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.UpdateComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    }
                }
                else
                {
                    if (frmModel.insertPhieuXK(curProdExport, sUserName))
                    {
                        InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                    }
                    else
                        InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
            else
                InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.REQUIRED_FIELD), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
        }

        private void checkBox_Pass_CheckedChanged(object sender, EventArgs e)
        {
            isPassed = checkBox_Pass.Checked;
        }

        private void dgvProdductList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvProdductList.CurrentCell != null && dgvProdductList.CurrentCell.ColumnIndex == 0 && e.RowIndex > -1)
                frmModel.addRemoveCheckedList(0, frmModel.getCheckedRow(e.RowIndex));

        }

        private void dgvProdductList_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            int num1_index = dgvProdductList.CurrentRow.Cells[PHIEU_XK_TP_CT_DTO.N_SL_THUC_XUAT].ColumnIndex;

            if (e.ColumnIndex == num1_index)
            {
                string s = UserValidator.isValidNumer(e.FormattedValue);
                e.Cancel = !string.IsNullOrEmpty(s);
                if (e.Cancel)
                {
                    InforDialog.showWarningrBox(Owner, trans.getMessageCaption(s), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }
        #endregion

        #region Utility Methods
        public void initExistData(DataGridViewRow selectRow)
        {
            this.selectRow = selectRow;
            isNew = false;
        }

        public void initDefaultData()
        {
            frmModel = new SI06FormModel();
            trans.controlTranslate(this);
            frmModel.translator = this.trans;

            uAuth = new UserAuthenticate();
            uAuth.setRights(this, sUserName);

            List<System.Reflection.PropertyInfo> properties = new List<System.Reflection.PropertyInfo>();
            PHIEU_XK_TP_CT_DTO temp1 = new PHIEU_XK_TP_CT_DTO();
            PHIEU_XK_TP_DTO temp2 = new PHIEU_XK_TP_DTO();
            properties.Add(temp1.GetType().GetProperty(PHIEU_XK_TP_CT_DTO.N_CHON));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XK_TP_CT_DTO.N_SO_LO));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XK_TP_CT_DTO.N_MA_MAU));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XK_TP_CT_DTO.N_DON_VI));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XK_TP_CT_DTO.N_SL_THUC_XUAT));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XK_TP_CT_DTO.N_QL_KHO));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XK_TP_CT_DTO.N_DON_VI_LANH));
            properties.Add(temp1.GetType().GetProperty(PHIEU_XK_TP_CT_DTO.N_GHI_CHU));
            properties.Add(temp2.GetType().GetProperty(PHIEU_XK_TP_DTO.N_MS_PHIEU_XH));

            List<string> readOnlyCols = new List<string>();
            readOnlyCols.Add(PHIEU_XK_TP_CT_DTO.N_SO_LO);
            readOnlyCols.Add(PHIEU_XK_TP_CT_DTO.N_MA_MAU);

            frmModel.buildDataGrid(dgvProdductList, properties, readOnlyCols);
            dgvProdductList.Columns[PHIEU_XK_TP_DTO.N_MS_PHIEU_XH].Visible = false;
            if (!isNew)
            {
                textBox_MSCT.Text = selectRow.Cells[PHIEU_XK_TP_DTO.N_MS_PHIEU_XK_TP].EditedFormattedValue.ToString();
                textBox_MSCT.Enabled = false;
                DateTime ngayXK = DateTime.Today;
                if (DateTime.TryParse(selectRow.Cells[PHIEU_XK_TP_DTO.N_NGAY_XK].Value.ToString(), out ngayXK))
                {
                    picNgayXK.Value = ngayXK;
                    picNgayXK.Enabled = false;
                }
                isPassed = (bool)selectRow.Cells[PHIEU_XK_TP_DTO.N_PHE_DUYET].EditedFormattedValue;
                checkBox_Pass.Checked = isPassed;

                frmModel.newProdExportDetail = frmModel.searchProdExportDetail(textBox_MSCT.Text);
            }
            else
            {
                frmModel.newProdExportDetail = frmModel.buildEmptyDataTable(properties);
            }

            frmModel.newProdExportDetail.PrimaryKey = new DataColumn[] { frmModel.newProdExportDetail.Columns[PHIEU_XK_TP_CT_DTO.N_SO_LO] };
            dgvProdductList.DataSource = frmModel.newProdExportDetail;
        }
        #endregion
    }
}
